package org.execute.tool.excel转为viso文件;

import java.util.ArrayList;
import java.util.List;

public class main {
    /**
     * 将excel文件转为viso文件
     * 原理：读取每一行 判断断在每一行最开始有多少个空白格，判断其级别，和归属，属于最靠近的父级的子级，子级
     * <bs:Brainstorm xmlns:bs="http://schemas.microsoft.com/visio/2003/brainstorming">
     *     <bs:topic bs:TopicID="T1">
     *         <bs:text>Java</bs:text>
     *         <bs:topic bs:TopicID="T1.1">
     *             <bs:text>基础语法</bs:text>
     *         </bs:topic>
     *     </bs:topic>
     *     <bs:topic bs:TopicID="T1.2">
     *          <bs:text>面向对象</bs:text>
     *     </bs:topic>
     *  </bs:topic>
     *  </bs:Brainstorm>
     *
     * 1提取出所有数据作二维数组
     * 2判断每一行最开始有多少个空格
     * 3和上一次判断的数据做对比，如果小于则是加入子级。如果等于则为同级，如果大于则依次判断是否是其子级
     * 4读取完后生成xml文件
     */

    public static void main(String[] args) {
        String excelFilePath = "C:\\Users\\liukai\\Desktop\\思维导图.xlsx";
        String writeToXML = "E:\\test\\思维导图.xml";
        String sheetName = "Sheet1";
        ExcelRangeExtractor excelRangeExtractor = new ExcelRangeExtractor();
        String[][] allData = excelRangeExtractor.extractAllAsArray(excelFilePath, sheetName);
        Element originElement = getElement(allData);
        List<String> xml = ElementToBrainstormXml.generateXml(originElement.getSubLevel());

        System.out.println(xml);

        FileListWriter.writeListToFile(writeToXML, xml);
    }

    private static Element getElement(String[][] allData) {
        Element originElement = new Element();
        Element currentElement = new Element();
        currentElement.setNumberOfSpaces(-1);
        currentElement.setSubLevel(new ArrayList<Element>());
        originElement = currentElement;
        for (int i = 0; i <= allData.length - 1; i++) {
            String[] row = allData[i];
            String content = "";
            int numberOfSpaces = 0;
            for (int j = 0; j < row.length; j++) {
                if (row[j].equals("")) {
                    numberOfSpaces++;
                } else {
                    content = row[j];
                    break;
                }
            }

            currentElement= addElement(currentElement, numberOfSpaces, content);
        }
        return originElement;
    }

    /**
     * 加入Element
     */
    public static Element addElement(Element element, int numberOfSpaces, String content) {
        if (element.getNumberOfSpaces() == numberOfSpaces) {
            //同级
            Element currentElement = new Element(numberOfSpaces,content, new ArrayList<Element>());
            String id;
            if (element.getNumberOfSpaces()!= -1){
                 id = element.getParentLevel().getId() + "." + (element.getParentLevel().getSubLevel().size() + 1);
            }else {
                id = ""+(element.getParentLevel().getSubLevel().size() + 1);
            }
            currentElement.setId(id);

            element.getParentLevel().getSubLevel().add(currentElement);
            currentElement.setParentLevel(element.getParentLevel());
            return currentElement;
        } else if (element.getNumberOfSpaces() > numberOfSpaces) {
            //父级
            Element parent = element.getParentLevel();
            return addElement(parent, numberOfSpaces, content);
        } else {
            //子级
            Element currentElement = new Element(numberOfSpaces, content, new ArrayList<Element>());
            String id;
            if (element.getNumberOfSpaces()!= -1){
                id = element.getId() + "." + (element.getSubLevel().size() + 1);
            }else {
                id = ""+(element.getSubLevel().size() + 1);
            }
            currentElement.setId(id);
            element.getSubLevel().add(currentElement);
            currentElement.setParentLevel(element);
            return currentElement;
        }
    }
}
